package com.enjoy.game.xcard;

import java.io.FileInputStream;
import java.io.InputStream;

import org.andengine.engine.camera.BoundCamera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.Entity;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.font.IFont;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.color.Color;

import com.enjoy.game.xcard.action.DragController;
import com.enjoy.game.xcard.action.MovementController;
import com.enjoy.game.xcard.sprite.Battle;
import com.enjoy.game.xcard.sprite.CellLayout;
import com.enjoy.game.xcard.sprite.CellLayout.CellFullListner;
import com.enjoy.game.xcard.sprite.DeckArea;
import com.enjoy.game.xcard.sprite.DeckBuilder;
import com.enjoy.game.xcard.sprite.XCard;

import android.opengl.GLES20;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.Menu;

public class MainActivity extends BaseGameActivity implements IOnMenuItemClickListener {
	static String XCARD_SERVER = "weiwangzi.com";
	protected static final int MENU_START = 0;
	
	private static  int CAMERA_WIDTH = 480;
	private static  int CAMERA_HEIGHT = 800;
	BoundCamera mBoundChaseCamera;
	BitmapTextureAtlas mCardDeckTexture;
	IFont mFont;
	DeckBuilder mDeckBuilder;
	MenuScene mMenuScene;
	Table mTable;
	BitmapTextureAtlas mMenuTexture;
	TextureRegion mMenuStartTextureRegion;
	
	
	@Override
	public EngineOptions onCreateEngineOptions() {
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		CAMERA_WIDTH = dm.widthPixels;
		CAMERA_HEIGHT = dm.heightPixels;
		
		this.mBoundChaseCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

		return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, 
				new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mBoundChaseCamera);
	}
	

	@Override
	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws Exception {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		mCardDeckTexture = new BitmapTextureAtlas(this.getTextureManager(), 2048, 1024, TextureOptions.BILINEAR);
		mFont = FontFactory.create(getFontManager(), getTextureManager(), 256, 256, 
				Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, Color.RED_ABGR_PACKED_INT);
		mFont.load();
		
		mMenuTexture = new BitmapTextureAtlas(this.getTextureManager(), 256, 128, TextureOptions.BILINEAR);
		mMenuStartTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTexture, this, "menu_start.png", 0, 0);
		mMenuTexture.load();
		
		pOnCreateResourcesCallback.onCreateResourcesFinished();
		
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {
		mEngine.registerUpdateHandler(new FPSLogger());
		
		InputStream istream = getAssets().open("gridbattle.tmx");
		mTable = new Table(this, istream);
		createMenuScene();
		enterDeckBuilderPhase(mTable);
		pOnCreateSceneCallback.onCreateSceneFinished(mTable);
	}

	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		pOnPopulateSceneCallback.onPopulateSceneFinished();
		
	}
	
	void enterDeckBuilderPhase(Table table){
		mDeckBuilder = new DeckBuilder(this);
		CellLayout cardSelectLayout = table.getCardSelectedCellLayout();
		DeckArea deckArea = table.getDeckArea();
		mDeckBuilder.loadCards(mCardDeckTexture, mFont, cardSelectLayout, getVertexBufferObjectManager());

		cardSelectLayout.setMovementController(new MovementController(deckArea));
		deckArea.setMovementController(new MovementController(cardSelectLayout));
		deckArea.setFullListner(new CellFullListner() {
			
			@Override
			public void onCellFull(CellLayout layout) {
				if(!mTable.hasChildScene()){
					layout.setFullListner(null);
					mTable.setChildScene(mMenuScene, false, true, true);;
				}
				
			}
		});
	}
	
	void enterFightPhase(Table table){
		CellLayout cardSelectLayout = table.getCardSelectedCellLayout(); 
		table.unregisterTouchArea(cardSelectLayout);
		//table.detachChild(cardSelectLayout);
		cardSelectLayout.setVisible(false);
		Battle battle = table.getBattle();
		battle.setVisible(true);
		DeckArea deckArea = table.getDeckArea();
		deckArea.setMovementController(new DragController(battle));
		table.setOnAreaTouchTraversalFrontToBack();
	}
	
	@Override
	public boolean onMenuItemClicked(final MenuScene pMenuScene, final IMenuItem pMenuItem, final float pMenuItemLocalX, final float pMenuItemLocalY) {
		switch(pMenuItem.getID()) {
		case MENU_START:
			mMenuScene.closeMenuScene();
			enterFightPhase(mTable);
			return true;
		}
		return false;
	}
	
	protected void createMenuScene() {
		this.mMenuScene = new MenuScene(mBoundChaseCamera);

		final SpriteMenuItem startMenuItem = new SpriteMenuItem(MENU_START, mMenuStartTextureRegion, getVertexBufferObjectManager());
		startMenuItem.setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
		this.mMenuScene.addMenuItem(startMenuItem);
		this.mMenuScene.buildAnimations();
		this.mMenuScene.setBackgroundEnabled(false);
		this.mMenuScene.setOnMenuItemClickListener(this);
	}

}
